package com.wwwc.index.web.servlet;

import java.io.*;
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;
import java.net.*;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;

import com.wwwc.util.web.*;
import com.wwwc.index.web.ejb.database.*;

public class ShoppingBean {
    private EJBDatabaseQuery ejbdb;

    public StringBuffer listHotDeals(HttpServletRequest request, String mid, String table_name, 
				     String max_rows, String pid, 
				     boolean manager, boolean admin) {
	//String call_path = request.getServletPath();
	String call_path = request.getContextPath()+request.getServletPath();

	String query = "SELECT LIMIT "+(Integer.parseInt(pid)-1)*Integer.parseInt(max_rows)+" "+
		    max_rows+" * FROM "+table_name+" ORDER BY TIME_STAMP DESC;";
	
	StringBuffer asbf = new StringBuffer();
	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }

	    String wid       = null; //0
	    String date      = null; //1
	    String title     = null; //2
	    String price     = null; //3
	    String order_url = null; //4
	    String des_text  = null; //5
	    String features  = null; //6
	    String image     = null; //7
	    String rebate    = null; //8
	    String coupon    = null; //9
	    String coup_exp  = null; //10
	    String company   = null; //11
	    String comp_url  = null; //12
	    String r_product = null; //13
	    String r_price   = null; //14
	    String r_service = null; //15
	    String t_raters  = null; //16
	    String counter   = null; //17
		
	    ArrayList alist = ejbdb.select(query);

	    Iterator it  = alist.iterator();
	    ArrayList a  = null;
	    StringBuffer sbf = null;
	    int m = 0;
	    while (it.hasNext()) {
		m++;
		try {
		    sbf = new StringBuffer();
		    a = (ArrayList)it.next();
		    wid       = (String)a.get(0); 
		    date      = (String)a.get(1); 
		    title     = (String)a.get(2); 
		
		    price     = (a.get(3) == null) ? "" : (String)a.get(3); 
		    order_url = (a.get(4) == null) ? "" : (String)a.get(4);
		    des_text  = (a.get(5) == null) ? "" : (String)a.get(5);
		    features  = (a.get(6) == null) ? "" : (String)a.get(6);
		    image     = (a.get(7) == null) ? "" : (String)a.get(7);
		    rebate    = (a.get(8) == null) ? "" : (String)a.get(8);
		    coupon    = (a.get(9) == null) ? "" : (String)a.get(9);
		    coup_exp  = (a.get(10) == null) ? "" : (String)a.get(10);
		    company   = (a.get(11) == null) ? "" : (String)a.get(11);
		    comp_url  = (a.get(12) == null) ? "" : (String)a.get(12);
		    r_product = (a.get(13) == null) ? "" : (String)a.get(13);
		    r_price   = (a.get(14) == null) ? "" : (String)a.get(14);
		    r_service = (a.get(15) == null) ? "" : (String)a.get(15);
		    t_raters  = (a.get(16) == null) ? "" : (String)a.get(16);
		    counter   = (a.get(17) == null) ? "" : (String)a.get(17);

		    sbf.append("<table width=90% border=0>");
		    sbf.append("<tr>");
		    sbf.append("<td align='left'>");
		    sbf.append("&nbsp;&nbsp;<B>"+title+"</B>");

		    if(MyUserInputCheck.checkMoney(price)) {
			if(price.length()>0) {
			    if(Double.parseDouble(price)>0.0) {
				sbf.append("&nbsp;<B>$"+price+"</B>");
			    }
			}
		    }

		    sbf.append("</td>");

		    sbf.append("<td align='right' valign='top' width='100'>");
		    sbf.append(date.substring(0,10));
		    sbf.append("</td>");
		    sbf.append("</tr>");
		    sbf.append("</table>");

		    sbf.append("<table width=90%>");
		    sbf.append("<tr><td align='left' valign='top' colspan='2' style='BORDER-TOP:5px solid'>");
		    sbf.append("&nbsp;");
		    sbf.append("</td></tr>");

		    if(manager || admin) {
			sbf.append("<tr><td align='center' valign='top' colspan='2'>");
			sbf.append("<table><tr>");
			sbf.append(MyHtml.makeLinkCell("Edit", call_path+"?mid="+mid+"&aid=3-1&fid="+wid));
			if(admin) {
			    sbf.append(MyHtml.makeLinkCell("Delete", call_path+"?mid="+mid+"&aid=4-1&fid="+wid));
			}
			sbf.append("</tr></table>");
			sbf.append("</td></tr>");
		    }

		    sbf.append("<tr><td align='left' valign='top'>");

		    if(image != null && image.endsWith(";")) {
			image = image.substring(0, image.length()-1);
		    }

		    if(image == null || image.length()<5) {
			image = "/images/error.jpg";
		    }

		    sbf.append("<TABLE  cellSpacing='0' cellPadding='0' border='0'>");
		    sbf.append("<TR><TD valign='top' width='120'>");
		    sbf.append("<TABLE width=100 height=150 style='border: 1 solid #FFFFFF'>");
		    sbf.append("<TR><TD align=center style=\"background-color: #FFFFFF\">");
		    sbf.append("<A href="+call_path+"?mid="+mid+"&aid=1-1&wid="+wid+"&url="+order_url+" target=_blank>");
		    sbf.append("<img name='img"+m+"' src="+image+" width=90 onError=getDefaultImage(document.img"+m+");>");
		    sbf.append("</A></TD></TR>");
		    sbf.append("</TABLE>");
		    
		    sbf.append("</td>");

		    sbf.append("<td width=580>");
		    
		    sbf.append("<TABLE border='0'>");
		    sbf.append("<TR><TD><B>Company:</B>&nbsp;");
		    if(comp_url != null && comp_url.length()>0) {
			sbf.append("<A href="+call_path+"?mid="+mid+"&aid=1-1&wid="+wid+"&url="+comp_url+" target=_blank>");
			sbf.append(company+"</A></TD></TR>");
		    }
		    else {
			sbf.append(company+"</TD></TR>");
		    }

		    if(des_text.length()>0) {
			sbf.append("<TR><TD>"+title+"</TD></TR>");
			sbf.append("<TR><TD>"+des_text+"</TD></TR>");
		    }
		    if(features.length()>0) {
			sbf.append("<TR><TD><B>Features:</B></TD></TR>");
			sbf.append("<TR><TD>"+MyHtmlDisplay.textToList(features, ";")+"</TD></TR>");
		    }
		    if(order_url != null && order_url.length()>0) {
			sbf.append("<TR><TD><A href="+call_path+"?mid="+mid+"&aid=1-1&wid="+wid+"&url="+order_url+" target=_blank>");
			sbf.append("<B>Order</B></A></TD></TR>");
		    }
		    if(rebate != null && rebate.length()>0) {
			sbf.append("<TR><TD><A href="+rebate+" target=_blank>");
			sbf.append("<B>Rebate</B></A></TD></TR>");
		    }
		    if(coupon.length()>0) {
			sbf.append("<TR><TD><B>Coupon Code:</B>"+coupon+"<BR><B>Expiration date:</B>"+coup_exp+"</TD></TR>");
		    }

		    sbf.append("</TABLE>");
		    
		    sbf.append("</TD>");
		    sbf.append("</TR>");
		    sbf.append("</TABLE>");

		    sbf.append("</td></tr>");
		    sbf.append("</table><BR><BR>");
		}
		catch (Exception e) {
		    sbf = null;
		    System.out.println("ShoppingBean.listHotDeals:Error:"+e);
		}

		if(sbf != null) {
		    sbf.append("<SCRIPT>");
		    sbf.append("function getDefaultImage(which) {");
		    sbf.append("  which.src=\"/images/error.jpg\";");
		    sbf.append("}");
		    sbf.append("</SCRIPT>");
		    asbf.append(sbf);
		}
	    }
	}
	catch (Exception e) {
	    System.out.println("ShoppingBean:Error:listHotDeals:"+e);
	}
	return asbf;
    }

    public StringBuffer addNewHotDeal(HttpServletRequest request, HttpServletResponse response, 
				      HttpSession session, String mid, String save_call_back, String saved_call_back) {

	StringBuffer sbf = new StringBuffer("");
	try {
	    String call_path = request.getContextPath()+request.getServletPath();
	    String submit_id    = request.getParameter("submit_id");
	    String title        = null;
	    String price        = null;
	    String tax          = null;
	    String shipping     = null;
	    String order_link   = null;
	    String description  = null;
	    String features     = null;
	    String image_link   = null;
	    String rebate_link  = null;
	    String coupon_code  = null;
	    String coupon_expe  = null;
	    String company_name = null;
	    String company_url  = null;
	    
	    StringBuffer error  = null;
	    
	    if(submit_id == null) {
		submit_id = ""+System.currentTimeMillis();
		session.setAttribute("submit_id", submit_id);
	    }
	    else {
		String submit_id_in_session = (String)session.getAttribute("submit_id");
		if(submit_id_in_session == null || !submit_id_in_session.equals(submit_id)) {
		    return (sbf);
		}

		String submited_id = (String)session.getAttribute("submited_id");
		if(submited_id != null && submited_id.equals(submit_id)) {
		    return (new StringBuffer("<BR><BR><H3>Please do not re-submit.</H3>"));
		}

		title        = request.getParameter("f0");
		price        = request.getParameter("f1");
		order_link   = request.getParameter("f2");
		description  = request.getParameter("f3");
		features     = request.getParameter("f4");
		image_link   = request.getParameter("f5");
		rebate_link  = request.getParameter("f6");
		coupon_code  = request.getParameter("f7");
		coupon_expe  = request.getParameter("f8");
		company_name = request.getParameter("f9");
		company_url  = request.getParameter("f10");

		error = addNewHotDealInputCheck(request);

		if(error == null) {
		    try{
			Vector vr = new Vector();
			vr.add(title);
			vr.add(price);        
			vr.add(order_link);   
			vr.add(description);  
			vr.add(features);     
			vr.add(image_link);   
			vr.add(rebate_link);  
			vr.add(coupon_code);  
			vr.add(coupon_expe);  
			vr.add(company_name); 
			vr.add(company_url);  

			if (ejbdb == null) {
			    ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
			}
			if(ejbdb.addNewHotDeal(vr)) {
			    vr = null;
			    sbf = null;
			    session.setAttribute("submited_id",submit_id);
			    
			    RequestDispatcher rd = request.getRequestDispatcher(call_path+"?mid="+mid+"&aid="+saved_call_back); 
			    rd.forward(request,response);
			    
			    //return new StringBuffer(call_path+"?mid="+mid+"&aid="+saved_call_back);
			}
			else {
			    error = new StringBuffer("Save data error. Please try again!");
			}
		    }
		    catch (Exception e) {
			error = new StringBuffer("Save data error. Please try again!");
		    }
		}
	    }

	    sbf = addNewHotDealForm(call_path, save_call_back, submit_id, mid, error, title, price, 
				    order_link, description, features, image_link, 
				    rebate_link, coupon_code, coupon_expe, company_name, company_url);
	}
	catch (Exception e) {
	    System.out.println("ShoppingBean:addNewHotDeal:Error:"+e);
	}
	return sbf;
    }

    private StringBuffer addNewHotDealForm(String form_call_back, String save_call_back, String submit_id, 
					   String mid, StringBuffer error, String title, String price, 
					   String order_link, String description, String features, 
					   String image_link, String rebate_link, String coupon_code, 
					   String coupon_expe, String company_name, String company_url) {
	StringBuffer sbf = new StringBuffer();
	try {
	    sbf.append("<BR>");
	    sbf.append("<FORM method='post' action='"+form_call_back+"'>");
	    sbf.append("<TABLE width='100%' align='center'>");
	    sbf.append("<TR><TD colspan='3' align='center'><H3>Add new hot deals</H3></TD</TR>");
	    
	    if(error != null && error.length()>0) {
		sbf.append("<TR><TD colspan='3' align='center'>");
		sbf.append("<img src='/images/star.gif' border='0'>");
		sbf.append("<I>"+error+"</I></TD</TR>");
	    }

	    sbf.append("<TR><TD align='right'><B>Product name:</B></TD>");
	    sbf.append("<TD align='left'><img src=/images/p_star.gif border='0'></TD>");
	    sbf.append("<TD align='left'>"+MyHtml.getTextBox("f0", title, 70, 250)+"</TD>");
	    sbf.append("</TR>");

	    sbf.append("<TR><TD align='right'><B>Price:</B></TD>");
	    sbf.append("<TD align='left'></TD>");
	    sbf.append("<TD align='left'>"+MyHtml.getTextBox("f1", price, 10, 10)+"</TD>");
	    sbf.append("</TR>");

	    sbf.append("<TR><TD align='right'><B>Order Link:</B></TD>");
	    sbf.append("<TD align='left'><img src=/images/p_star.gif border='0'></TD>");
	    sbf.append("<TD align='left'>"+MyHtml.getTextBox("f2", order_link, 70, 500)+"</TD>");
	    sbf.append("</TR>");
   
	    sbf.append("<TR><TD align='right'><B>Description:</B></TD>");
	    sbf.append("<TD align='left'><img src=/images/p_star.gif border='0'></TD>");
	    sbf.append("<TD align='left'>"+MyHtml.getTextArea("f3", description, 5, 50)+"</TD>");
	    sbf.append("</TR>");

	    sbf.append("<TR><TD></TD>");
	    sbf.append("<TD></TD>");
	    sbf.append("<TD align='left'><I>Please use \"<B>;</B>\" to separate each feature.</I></TD>");
	    sbf.append("</TR>");

	    sbf.append("<TR><TD align='right'><B>Features:</B></TD>");
	    sbf.append("<TD align='left'><img src=/images/p_star.gif border='0'></TD>");
	    sbf.append("<TD align='left'>"+MyHtml.getTextArea("f4", features, 5, 50)+"</TD>");
	    sbf.append("</TR>");

	    sbf.append("<TR><TD align='right'><B>Image Link:</B></TD>");
	    sbf.append("<TD align='left'></TD>");
	    sbf.append("<TD align='left'>"+MyHtml.getTextBox("f5", image_link, 70, 500)+"</TD>");
	    sbf.append("</TR>");

	    sbf.append("<TR><TD align='right'><B>Rebate Link:</B></TD>");
	    sbf.append("<TD align='left'></TD>");
	    sbf.append("<TD align='left'>"+MyHtml.getTextBox("f6", rebate_link, 70, 500)+"</TD>");
	    sbf.append("</TR>");

	    sbf.append("<TR><TD align='right'><B>Coupon Code:</B></TD>");
	    sbf.append("<TD align='left'></TD>");
	    sbf.append("<TD align='left'>"+MyHtml.getTextBox("f7", coupon_code, 10, 50)+"</TD>");
	    sbf.append("</TR>");

	    sbf.append("<TR>");
	    sbf.append("<TD align='right'><B>Coupon Exp.:</B></td>");
	    sbf.append("<TD></TD>");
	    sbf.append("<TD>"+MyHtml.getTextBox("f8", coupon_expe, 10, 10)+"</TD>");
	    sbf.append("</TR>");

	    sbf.append("<TR><TD align='right'><B>Company Name:</B></TD>");
	    sbf.append("<TD align='left'><img src=/images/p_star.gif border='0'></TD>");
	    sbf.append("<TD align='left'>"+MyHtml.getTextBox("f9", company_name, 70, 250)+"</TD>");
	    sbf.append("</TR>");

	    sbf.append("<TR><TD align='right'><B>Company URL:</B></TD>");
	    sbf.append("<TD align='left'></TD>");
	    sbf.append("<TD align='left'>"+MyHtml.getTextBox("f10", company_url, 70, 500)+"</TD>");
	    sbf.append("</TR>");

	    sbf.append("<TR>");
	    sbf.append("<TD align='center' colspan='3'>");
	    sbf.append("<input type='submit' name='B' value='Submit' style='font-size: 8pt'>");
	    sbf.append("</TD>");
	    sbf.append("</TR>");
	    sbf.append("<input type='hidden' name='submit_id' value='"+submit_id+"'>");
	    sbf.append("<input type='hidden' name='mid' value='"+mid+"'>");
	    sbf.append("<input type='hidden' name='aid' value='"+save_call_back+"'>");

	    sbf.append("</TABLE>");
	    sbf.append("</FORM>");
	}
	catch (Exception e) {
	    System.out.println("ShoppingBean:addNewHotDeal:Error:"+e);
	}
	return sbf;
    }

    private StringBuffer addNewHotDealInputCheck(HttpServletRequest request) {
	StringBuffer error = null;
	try {
	    String title        = request.getParameter("f0");
	    String price        = request.getParameter("f1");
	    String order_link   = request.getParameter("f2");
	    String description  = request.getParameter("f3");
	    String features     = request.getParameter("f4");
	    String image_link   = request.getParameter("f5");
	    String rebate_link  = request.getParameter("f6");
	    String coupon_code  = request.getParameter("f7");
	    String coupon_expe  = request.getParameter("f8");
	    String company_name = request.getParameter("f9");
	    String company_url  = request.getParameter("f10");
		
	    if(title.length() ==0) {
		error = new StringBuffer();
		error.append("Field [Product Name] is required!");
	    }

	    if(error == null && price.length()>0) {
		if(!MyUserInputCheck.checkMoney(price)) {
		    error = new StringBuffer("Error found at filed [Price].");
		}
	    }

	    if(error == null) {
		if(order_link.length() == 0) {
		    error = new StringBuffer("Field [Order Link] is required.");
		}
		else {
		    if(!MyUserInputCheck.isUrl(order_link)) {
			error = new StringBuffer("Error found at field [Order Link].");
		    }
		}
	    }

	    if(error == null) {
		if(description.length() == 0) {
		    error = new StringBuffer("Field [Description] is required.");
		}
		else {
		    if(description.length()> 2000) {
			error = new StringBuffer("Field [Description] is too long (2000 characters or less).");
		    }
		}
	    }

	    if(error == null) {
		if(features.length() == 0) {
		    error = new StringBuffer("Field [Features] is required.");
		}
		else {
		    if(features.length()> 2000) {
			error = new StringBuffer("Field [Features] is too long (2000 characters or less).");
		    }
		}
	    }
	    /*
	    if(error == null) {
		if(image_link.length() > 0) {
		    if(!MyUserInputCheck.isUrl(image_link)) {
			error = new StringBuffer("Error found at field [Image Link].");
		    }
		}
	    }
	    */

	    if(error == null) {
		if(rebate_link.length() > 0) {
		    if(!MyUserInputCheck.isUrl(rebate_link)) {
			error = new StringBuffer("Error found at field [Rebate Link].");
		    }
		}
	    }

	    if(error == null) {
		if(coupon_expe.length() > 0) {
		    if(!MyUserInputCheck.checkDate(coupon_expe)) {
			error = new StringBuffer("Error found at field [Coupon Exp.]! Please use mm/dd/yyyy format.");
		    }
		}
	    }

	    if(error == null) {
		if(company_name.length() == 0) {
		    error = new StringBuffer("Field [Company Name] ir required.");
		}
	    }

	    if(error == null) {
		if(company_url.length() > 0) {
		    if(!MyUserInputCheck.isUrl(company_url)) {
			error = new StringBuffer("Error found at field [Company URL].");
		    }
		}
	    }
	}
	catch (Exception e) {
	    error = new StringBuffer("System Error. Please try again!");
	    System.out.println("ShoppingBean:addNewHotDealInputCheck:"+e);
	}
	return error;
    }

    public StringBuffer editHotDeal(HttpServletRequest request, HttpServletResponse response, 
				    HttpSession session, String mid, String edit_call_back, String updated_call_back) {
	StringBuffer sbf = new StringBuffer();
	//String call_path = request.getServletPath();
	String call_path = request.getContextPath()+request.getServletPath();

	String wid       = null; //0
	String date      = null; //1
	String title     = null; //2
	String price     = null; //3
	String order_url = null; //4
	String des_text  = null; //5
	String features  = null; //6
	String image     = null; //7
	String rebate    = null; //8
	String coupon    = null; //9
	String coup_exp  = null; //10
	String company   = null; //11
	String comp_url  = null; //12
	String r_product = null; //13
	String r_price   = null; //14
	String r_service = null; //15
	String t_raters  = null; //16
	String counter   = null; //17

	try {
	    String fid = null; 
	    String submit_id = request.getParameter("submit_id");
	    StringBuffer error = null;

	    if(submit_id == null) {
		fid = request.getParameter("fid");
		if(fid == null) {
		    return null;
		}
		else {
		    session.setAttribute("edit_hot_deal_fid", fid);
		}
		submit_id = ""+System.currentTimeMillis();
		session.setAttribute("submit_id", submit_id);

		if (ejbdb == null) {
		    ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
		}
		
		ArrayList a  = ejbdb.selectHotDeal(fid);
		if(a == null || a.size()==0) {
		    return null;
		}

		wid       = (String)a.get(0); 
		date      = (String)a.get(1); 
		title     = (String)a.get(2); 
		
		price     = (a.get(3) == null) ? "" : (String)a.get(3); 
		order_url = (a.get(4) == null) ? "" : (String)a.get(4);
		des_text  = (a.get(5) == null) ? "" : (String)a.get(5);
		features  = (a.get(6) == null) ? "" : (String)a.get(6);
		image     = (a.get(7) == null) ? "" : (String)a.get(7);
		rebate    = (a.get(8) == null) ? "" : (String)a.get(8);
		coupon    = (a.get(9) == null) ? "" : (String)a.get(9);
		coup_exp  = (a.get(10) == null) ? "" : (String)a.get(10);
		company   = (a.get(11) == null) ? "" : (String)a.get(11);
		comp_url  = (a.get(12) == null) ? "" : (String)a.get(12);
		r_product = (a.get(13) == null) ? "" : (String)a.get(13);
		r_price   = (a.get(14) == null) ? "" : (String)a.get(14);
		r_service = (a.get(15) == null) ? "" : (String)a.get(15);
		t_raters  = (a.get(16) == null) ? "" : (String)a.get(16);
		counter   = (a.get(17) == null) ? "" : (String)a.get(17);

		if(Double.parseDouble("0"+price) == 0) {
		    price = "";
		} 
	    }
	    else {
		fid = (String)session.getAttribute("edit_hot_deal_fid");
		error = addNewHotDealInputCheck(request);

		title     = request.getParameter("f0");
		price     = request.getParameter("f1");
		order_url = request.getParameter("f2");
		des_text  = request.getParameter("f3");
		features  = request.getParameter("f4");
		image     = request.getParameter("f5");
		rebate    = request.getParameter("f6");
		coupon    = request.getParameter("f7");
		coup_exp  = request.getParameter("f8");
		company   = request.getParameter("f9");
		comp_url  = request.getParameter("f10");

		if(error == null) {
		    try{
			Vector vr = new Vector();
			vr.add(fid);
			vr.add(title);
			vr.add(price);        
			vr.add(order_url);   
			vr.add(des_text);  
			vr.add(features);     
			vr.add(image);   
			vr.add(rebate);  
			vr.add(coupon);  
			vr.add(coup_exp);  
			vr.add(company); 
			vr.add(comp_url);  
			
			if (ejbdb == null) {
			    ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
			}
			
			if(ejbdb.updateHotDeal(vr)) {
			    vr = null;
			    sbf = null;

			    RequestDispatcher rd = request.getRequestDispatcher(call_path+"?mid="+mid+"&aid="+updated_call_back); 
			    rd.forward(request,response);
			}
			else {
			    error = new StringBuffer("Update data error. Please try again!");
			}
		    }
		    catch (Exception e) {
			error = new StringBuffer("Update data error. Please try again!");
		    }
		}
	    }

	    sbf = addNewHotDealForm(call_path, edit_call_back, submit_id, mid, error, title, price, 
				    order_url, des_text, features, image, 
				    rebate, coupon, coup_exp, company, comp_url);
	}
	catch (Exception e) {
	    System.out.println("ShoppingBean:Error:editHotDeals:"+e);
	}
	return sbf;
    }

    public void deleteHotDeal(HttpServletRequest request, HttpServletResponse response, 
				  String mid, String call_back) {
	//String call_path = request.getServletPath();
	String call_path = request.getContextPath()+request.getServletPath();
	String table_name = request.getParameter("table_name");
	String fid = request.getParameter("fid");

	String query = "DELETE FROM "+table_name+" WHERE ID='"+fid+"';";

	try {

	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }

	    ejbdb.delete(query);
	    RequestDispatcher rd = request.getRequestDispatcher(call_path+"?mid="+mid+"&aid="+call_back); 
	    rd.forward(request,response);
	}
	catch (Exception e) {
	    System.out.println("ShoppingBean:deleteHotDeals:Error:"+e);
	}
    }

    public void updateHotDealsCounter (String wid, String table_name) {
	if(ejbdb == null) {
	    ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	}

	try {
	    String query = "SELECT COUNTER FROM "+table_name+" WHERE ID="+wid+";";
	    String temp = ejbdb.selectSingleFieldValue(query, "COUNTER");
	    int count = 1;
	    if(temp != null && temp.length()>0) {
		count = Integer.parseInt(temp);
	    }
	    
	    query = "UPDATE "+table_name+" SET COUNTER="+(count+1)+" WHERE ID="+wid+";";
	    ejbdb.update(query);
	}
	catch(IOException e) {
	    System.out.println("TopBerBean:updateHotDealsCounter:Error:"+e);
	}
    }
 }






